<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Customer Comment Manager
 * @author	Nguyen Viet Manh
 * @mail	manhnv85@gmail.com
 * @date	16.03.2012
 */
require_once('admin'.EXT);

class Customer_comment extends Admin {
    
    function __construct()
    {
        parent::__construct();
        $this->load->model(array('customer_comment_model'));
        $this->load->helper(array('upload','text'));		
    }
    
    function index()
    {
        $rows   =   $this->customer_comment_model->get_all();
        $this->data['rows'] =   $rows;
        $this->data['tpl_file']	= 'customer_comment/index';
        $this->load->view('layout/default', $this->data);
    }
    
    function update($id = null)
    {
        $row    =   array();
        $input_form =   array();
        if($id)
        {
            $row    =   $this->customer_comment_model->get_by('id',$id);
            $input_form['fullname'] =   $row->fullname;
            $input_form['content'] =   $row->content;
            $input_form['info'] =   $row->info;
            $input_form['active'] =   $row->active;
        }
        
        //
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $fullname   =   $this->input->post('fullname');
            $info   =   $this->input->post('info');
            $suc    =   false;
            $content   =   $this->input->post('content');
            $active   =   ($this->input->post('active') == 1 ? 1 : 0);
            $data   =   array('fullname' => $fullname, 'info' => $info, 'content' => $content, 'active' => $active);
            if(isset($_FILES["image"]["name"]) && $_FILES["image"]["name"] != '') 
        	{
        		// Upload images
        		$album_dir = 'uploads/customer_comment/';
        		if(!is_dir($album_dir))
        		{            			
        			create_dir($album_dir);	
        		}
        		
        	    $ext = get_ext($_FILES["image"]["name"]);
                if(!in_array($ext, array('png', 'gif', 'jpg', 'jpeg'))) {
                	continue;
                }
               
        		if($_FILES['image']['error'] === 0) 
        	    {            	    	
			        $new_path = $album_dir.$_FILES["image"]["name"];
			        move_uploaded_file($_FILES["image"]['tmp_name'], dirname($_SERVER['SCRIPT_FILENAME']).'/'.$new_path);   			            	    
        			$data['image']	=	$this->_create_thumb($new_path);
        	    }
        	                	        	
        	}
            $input_form =   $data;
            
            $this->form_validation->set_message('required', '%s không hợp lệ!');
            $this->form_validation->set_rules('fullname','Họ tên','required');
            
            //No error
            if($this->form_validation->run() == TRUE)
            {
                if($row)
                {
                    if($this->customer_comment_model->update($id,$data))
                    {
                        $suc    =   true;
                    }
                }//End update by id
                else
                {
                    if($this->customer_comment_model->insert($data))
                    {
                        $suc    =   true;
                    }
                }//End add new
                
            }
            else//show error
            {
                $this->message->add('error',validation_errors());
            }
            
            //Success
            if($suc)
            {
                redirect(admin_url('customer_comment'));
            }
        }
        
        $this->data['input_form']   =   $input_form;
        $this->data['tpl_file']	= 'customer_comment/update';
        $this->load->view('layout/default', $this->data);
    }
    
    /*
     * method: create thumb
     * param: path image
     * return: path/image_name
     */
	function _create_thumb($img_path = '')
	{
		$sizes = config_item('thumb_img_customer_comment');
		$this->load->helper('string');
		$pathinfo = pathinfo($img_path);
		$new_name = $pathinfo['basename'];
		$temp	=	explode('.',$pathinfo['basename']);
		$new_name	=	$temp[0].date('-his-mdy').'.'.$pathinfo['extension'];

		rename($img_path, $pathinfo['dirname'].'/'.$new_name);

        include_once (config_item('phpThumb_dir')."ThumbLib.inc.php");

        $options = array(
		    'resizeUp' 		=> true,
		    'jpegQuality'	=> 88,
        );

		
		foreach($sizes as $size)
		{
			$thumb_dir  = $pathinfo['dirname'].'/thumb_'.$size;
			create_dir($thumb_dir);

			$t_size = explode('-', $size);

		    $thumb 	= PhpThumbFactory::create($pathinfo['dirname'].'/'.$new_name, $options);

		    $thumb->adaptiveResize($t_size[0] + 5, $t_size[1] + 5);
		    $thumb->cropFromCenter($t_size[0], $t_size[1]);
		    $thumb->save($thumb_dir.'/'.$new_name, 'jpg');
		}
		return $pathinfo['dirname'].'/thumb_'.$sizes[0].'/'.$new_name;
	}
	//End create thumb
    
}